package com.hdaccp.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.hdaccp.dao.CapitalDao;
import com.hdaccp.dao.StatusDao;
import com.hdaccp.dao.ZongDao;
import com.hdaccp.dao.impl.CapitalDaoimpl;
import com.hdaccp.dao.impl.StatusDaoimpl;
import com.hdaccp.dao.impl.ZongDaoimpl;
import com.hdaccp.entity.Capital;
import com.hdaccp.entity.Status;

@WebServlet(urlPatterns="/zijin.do")
public class zijinservlet extends HttpServlet {

	private CapitalDao cp=new CapitalDaoimpl();
	/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int mm=Integer.parseInt(request.getParameter("mm"));
		if(mm==1){
			all(request,response);
			
		}else if(mm==2){
			xiu(request,response);
			
		}else if(mm==3){
			add(request,response);
		}else if(mm==4){
			gaimoney(request,response);
		}else if(mm==5){
			tianhe(request,response);
		}else if(mm==6){
			del(request,response);
			
		}
		
	}
	int zid;
	public void all(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		 zid=Integer.parseInt(request.getParameter("zid"));
		
		Capital c=cp.getYC(zid);
		StatusDao st=new StatusDaoimpl();
		List<Status> list=st.getYS(c.getId());
		double z=0;
		double ch=0;
		
		
		for (Status s : list) {
			double yi = (double)Math.round(s.getMaterials()*100)/100  ;
			double er = (double)Math.round(s.getMate()*100)/100  ;
			double san = (double)Math.round(s.getFee()*100)/100  ;
			double si = (double)Math.round(s.getOther()*100)/100  ;
			s.setMaterials(yi);
			s.setMate(er);
			s.setFee(san);
			s.setOther(si);
			
			
			
			
			z=s.getMaterials()+s.getMate()+s.getFee()+s.getOther();
			double wu = (double)Math.round(z*100)/100  ;
			s.setZong(wu);
			
			
			
			
			ch+=z;
		}
		double b=0;
		c.setChu(ch);
		b=ch/c.getSum()*100;
		double bbb=(double)Math.round(b*100)/100 ;
		c.setBai(bbb);
		request.setAttribute("zid", zid);
		request.setAttribute("c", c);
		request.setAttribute("list", list);
		request.getRequestDispatcher("zijin.jsp").forward(request, response);
	}
	public void xiu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		double jie=Double.parseDouble(request.getParameter("jie"));
		int id=Integer.parseInt(request.getParameter("id"));
		Capital c=new Capital();
		c.setSum(jie);
		c.setId(id);
		cp.updYC(c);
		
		
		riqi r=new riqi();
		r.update(request, response, zid);
		
		Gson gson=new Gson();
		PrintWriter out=response.getWriter();
		out.print(gson.toJson(jie));
		out.close();
	}
	public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int cid=Integer.parseInt(request.getParameter("cid"));
		double f=Double.parseDouble(request.getParameter("f"));
		double z=Double.parseDouble(request.getParameter("z"));
		double s=Double.parseDouble(request.getParameter("s"));
		double q=Double.parseDouble(request.getParameter("q"));
		double zong=f+z+s+q;
		Status ss=new Status();
		ss.setCid(cid);
		ss.setMaterials(f);
		ss.setMate(z);
		ss.setFee(s);
		ss.setOther(q);
		ss.setZong(zong);
		StatusDao std=new StatusDaoimpl();
		boolean flag=std.insYS(ss);
		ss=new Status();
		ss=std.getYID();
		
		ss.setZong(zong);
		
		
		riqi r=new riqi();
		r.update(request, response, zid);
		
		Gson gson=new Gson();
		PrintWriter out=response.getWriter();
		out.print(gson.toJson(ss));
		out.close();
	}
	public void gaimoney(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String money=request.getParameter("money");
		int zid=Integer.parseInt(request.getParameter("zid"));
		
		ZongDao zz=new ZongDaoimpl();
		boolean flag=zz.getYup(money, zid);
		Gson gson=new Gson();
		PrintWriter out=response.getWriter();
		out.print(gson.toJson(flag));
		out.close();
	}
	public void tianhe(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		double the=Double.parseDouble(request.getParameter("the"));
		double tjie=Double.parseDouble(request.getParameter("tjie"));
		int zid=Integer.parseInt(request.getParameter("zid"));
		Capital c=new Capital();
		c.setCamount(the);
		c.setSum(tjie);
		c.setZid(zid);
		CapitalDao cc=new CapitalDaoimpl();
		boolean flag=cc.updYXT(c);
		Gson gson=new Gson();
		PrintWriter out=response.getWriter();
		out.print(gson.toJson(c));
		out.close();
	}
	public void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int id=Integer.parseInt(request.getParameter("id"));
		int cid=Integer.parseInt(request.getParameter("cid"));
		
		StatusDao st=new StatusDaoimpl();
		boolean f=st.delY(id);
		PrintWriter out=response.getWriter();
		if(f){
			out.print(1);
		}else{
			out.print(2);
		}
		
		
		out.close();
		
	
		
		
	}
	
	/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		doGet(request,response);
	}

}
